/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estg.dae.projecto.broker.controllers;

import estg.dae.projecto.bank.impl.BankTO;
import estg.dae.projecto.bank.impl.ElectronicOperationTO;
import estg.dae.projecto.broker.data.BankDaoLocal;
import estg.dae.projecto.broker.model.Bank;
import java.util.Collection;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

/**
 *
 * @author mario
 */
@Stateless
@Remote(IBrokerControllerRemote.class)
public class BrokerSessionBean implements BrokerSessionLocal, IBrokerControllerRemote {

    @EJB
    BankDaoLocal bankDao;
    
    // <editor-fold defaultstate="collapsed" desc="Local methods implementation">
    public boolean registerBank(Bank bank) {
        bank.setActive(true);
        //TODO: Atribuir hora de transferências offline
        bank.setTimeOfflineTransfers(new Date(0, 0, 0, 1, 30, 0));
        bankDao.update(bank);

        return true;
    }
    public void unregisterBank(Bank bank) {
        bank.setActive(false);
        bankDao.update(bank);
    }
    public boolean isBankRegistered(Bank bank) {
        return bank.isActive();
    }

    public Collection<Bank> getAllBanks() {
        return bankDao.getAll();
    }

    public void saveBank(Bank bank) throws Exception {
        //verifica se o código do banco é válido
        if (bank.getCode().length()!=4)
            throw new Exception("Código do banco é inválido");

        try {
            Integer.parseInt(bank.getCode());
        }
        catch(NumberFormatException error) {
            throw new Exception("Código do banco é inválido");
        }

        bankDao.save(bank);
    }

    public void updateBank(Bank bank) {
        bankDao.update(bank);
    }

    public Bank getBank(long id) {
        return bankDao.get(id);
    }

    public Bank getBank(String code) {
        return bankDao.get(code);
    }

    public boolean existsBank(String code) {
        return bankDao.exists(code);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Remote methods implementation">
    //O registo de bancos devolve falso para permitir posteriormente negar o registo do banco
    //por algum motivo
    public boolean registerBank(BankTO bankTO) {
        Bank bank;

        if (bankDao.exists(bankTO.getCode()))
        {
            bank = bankDao.get(bankTO.getCode());
            if (bank.isActive())
                return true;

            //TODO: verificar se os dados estão actualizados
        } else {
            bank = new Bank(bankTO);
        }

        return registerBank(bank);
    }

    public void unregisterBank(String bankCode) {
        if (bankDao.exists(bankCode))
        {
            Bank bank = bankDao.get(bankCode);
            unregisterBank(bank);
        }
    }
    
    public boolean isBankRegistered(String bankCode) {
        if (bankDao.exists(bankCode))
        {
            Bank bank = bankDao.get(bankCode);
            return isBankRegistered(bank);
        }
        return false;
    }

    public boolean updateBank(BankTO bankTO) {
        Bank bank = bankDao.get(bankTO.getCode());

        //Se o banco já existir
        if (bank == null) {
            return false;
        }

        //TODO: Actualizar dados do banco
        
        updateBank(bank);
        return true;
    }

    public String registerCard(String bankCode, String clientAddress) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean suspendCard(String bankCode, String cardNumber) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean executeOfflineOperation(ElectronicOperationTO electronicOperation) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    // </editor-fold>
}
